package com.along.dev.rbac.mapper;

import com.along.dev.rbac.dto.DeptDTO;
import com.along.dev.rbac.entity.Dept;
import com.along.dev.rbac.qo.DeptQO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface DeptMapper {

  // 定义结果映射
  @Results(id = "deptMap", value = {
          @Result(property = "deptId", column = "dept_id", id = true),
          @Result(property = "parentId", column = "parent_id"),
          @Result(property = "ancestors", column = "ancestors"),
          @Result(property = "deptName", column = "dept_name"),
          @Result(property = "orderNum", column = "order_num"),
          @Result(property = "leader", column = "leader"),
          @Result(property = "phone", column = "phone"),
          @Result(property = "email", column = "email"),
          @Result(property = "status", column = "status"),
          @Result(property = "delFlag", column = "del_flag"),
          @Result(property = "createBy", column = "create_by"),
          @Result(property = "createTime", column = "create_time", jdbcType = JdbcType.TIMESTAMP),
          @Result(property = "updateBy", column = "update_by"),
          @Result(property = "updateTime", column = "update_time", jdbcType = JdbcType.TIMESTAMP)
  })

  // 排除指定部门及其子部门
  @Select("SELECT * FROM sys_dept WHERE dept_id != #{deptId} AND FIND_IN_SET(#{deptId}, ancestors) = 0 AND del_flag = '0'")
//  @ResultMap("deptMap")
  List<Dept> exclude(@Param("deptId") Integer deptId);

  // 查询部门列表（支持分页）
  @Select("<script>" +
          "SELECT * FROM sys_dept WHERE del_flag = '0'" +
          "<if test='deptName != null and deptName != \"\"'> AND dept_name LIKE CONCAT('%', #{deptName}, '%')</if>" +
          "<if test='status != null and status != \"\"'> AND status = #{status}</if>" +
          "<if test='parentId != null'> AND parent_id = #{parentId}</if>" +
          " ORDER BY order_num ASC" +
          "</script>")
  @ResultMap("deptMap")
  Page<Dept> find(DeptQO deptQO);

  // 保存部门信息
  @Insert("INSERT INTO sys_dept (" +
          "parent_id, ancestors, dept_name, order_num, leader, phone, email, " +
          "status, del_flag, create_by, create_time, update_by, update_time" +
          ") VALUES (" +
          "#{parentId}, #{ancestors}, #{deptName}, #{orderNum}, #{leader}, #{phone}, #{email}, " +
          "#{status}, #{delFlag}, #{createBy}, #{createTime}, #{updateBy}, #{updateTime}" +
          ")")
  @Options(useGeneratedKeys = true, keyProperty = "deptId")
  int save(Dept dept);

  // 更新部门信息
  @Update("<script>" +
          "UPDATE sys_dept " +
          "<set>" +
          "<if test='parentId != null'>parent_id = #{parentId},</if>" +
          "<if test='ancestors != null'>ancestors = #{ancestors},</if>" +
          "<if test='deptName != null'>dept_name = #{deptName},</if>" +
          "<if test='orderNum != null'>order_num = #{orderNum},</if>" +
          "<if test='leader != null'>leader = #{leader},</if>" +
          "<if test='phone != null'>phone = #{phone},</if>" +
          "<if test='email != null'>email = #{email},</if>" +
          "<if test='status != null'>status = #{status},</if>" +
          "<if test='delFlag != null'>del_flag = #{delFlag},</if>" +
          "<if test='updateBy != null'>update_by = #{updateBy},</if>" +
          "update_time = #{updateTime}" +
          "</set>" +
          " WHERE dept_id = #{deptId}" +
          "</script>")
  int update(Dept dept);

  // 删除部门信息（逻辑删除）
  @Update("UPDATE sys_dept SET del_flag = '2', update_time = NOW() WHERE dept_id = #{deptId}")
  int delete(@Param("deptId") Long deptId);

  // 根据部门名称查询（通用查询）
  @Select("SELECT * FROM sys_dept WHERE dept_name LIKE CONCAT('%', #{deptName}, '%') AND del_flag = '0'")
  @ResultMap("deptMap")
  List<Dept> commFindByName(@Param("deptName") Long deptName);

  // 根据部门ID查询
  @Select("SELECT * FROM sys_dept WHERE dept_id = #{deptId} AND del_flag = '0'")
  @ResultMap("deptMap")
  Dept findById(@Param("deptId") Long deptId);

  // 查询所有子部门
  @Select("SELECT * FROM sys_dept WHERE FIND_IN_SET(#{deptId}, ancestors) > 0 AND del_flag = '0'")
  @ResultMap("deptMap")
  List<Dept> findChildren(@Param("deptId") Long deptId);

  // 检查部门名称是否唯一
  @Select("SELECT COUNT(1) FROM sys_dept WHERE dept_name = #{deptName} AND parent_id = #{parentId} AND del_flag = '0'")
  int checkDeptNameUnique(@Param("deptName") String deptName, @Param("parentId") Long parentId);
}
